package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.mes.biz.assist.ext.Wrapperz;
import com.dnjn.mes.biz.domain.MesRegisterAddress;
import com.dnjn.mes.biz.enums.AddrCode;
import com.dnjn.mes.biz.service.IMesRegisterAddressService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * modbus地址对应表 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/biz/mesRegisterAddress")
public class MesRegisterAddressController extends BaseController {

    @Autowired
    protected IMesRegisterAddressService baseService;


    @GetMapping("/getByEquipmentId/{equipmentId}")
    public AjaxResult getByEquipmentId(@PathVariable Long equipmentId) {
        List<MesRegisterAddress> list = baseService.list(
                Wrapperz.lambdaQuery(MesRegisterAddress.class).eq(MesRegisterAddress::getEquipmentId, equipmentId));
        return success(list);
    }

    @GetMapping("/getByCode/{addrCode}")
    public AjaxResult getByCode(@PathVariable AddrCode addrCode) {
        return success(
                baseService.<MesRegisterAddress>lambdaQuery().eq(MesRegisterAddress::getAddrCode, addrCode).one());
    }

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesRegisterAddress domain) {
        domain.setCreateBy(getUsername());
        return toAjax(baseService.save(domain));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesRegisterAddress domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestBody MesRegisterAddress domain, @RequestParam(defaultValue = "1") Integer current,
                           @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<MesRegisterAddress> list = baseService.page(page, Wrappers.query(domain));
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, MesRegisterAddress domain) {
        List<MesRegisterAddress> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesRegisterAddress> util = new ExcelUtil<MesRegisterAddress>(MesRegisterAddress.class);
        util.exportExcel(response, list, "参数数据");
    }


    /* @Parameters({
             @Parameter(name = "addressId", description = "地址id" ),
             @Parameter(name = "AddrCode", description = "AddrCode" ),
             @Parameter(name = "addrPlc", description = "PLC地址" ),
             @Parameter(name = "addrModbus", description = "modbus地址" ),
             @Parameter(name = "duration", description = "间隔单位秒S" )
     })*/
    @Operation(summary = "基础信息地址设置")
    @PutMapping("/updateAddressByCode")
    public AjaxResult updateAddressByCode(@Validated @RequestBody ArrayList<MesRegisterAddress> mesRegisterAddress) {
        //Boolean flag = baseService.updateAddressByCode(mesRegisterAddress);
        try {
            for (MesRegisterAddress m : mesRegisterAddress) {
                Assert.notNull(m.getAddrPlc(), "PLC地址不能为空！");
                Assert.notNull(m.getDuration(), "间隔不能为空！");
                baseService.updateAddressByCode(m);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return toAjax(false);
        }
        return toAjax(true);
    }


}